package com.z_talk.client.core;

import com.z_talk.core.enums.ResponseCodeEnum;
import com.z_talk.core.enums.ResponseEnum;
import com.z_talk.core.message.ResponseMessage;
import com.z_talk.core.model.PrivateTalkModel;
import com.z_talk.core.model.UserInfo;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class ClientHandler extends ChannelHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ResponseMessage message = (ResponseMessage) msg;
        ResponseEnum messageType = message.getMessageType();
        Channel channel = ctx.channel();
        switch (messageType){
            case REGISTER_RESPONSE:
                ResponseCodeEnum registerResponse = message.getCodeEnum();
                String registerMsg = (String)message.getAttribute().get(ResponseEnum.REGISTER_RESPONSE.getAttr());
                MessageHandler.regiterHandler(channel,registerResponse,registerMsg);
                break;
            case LOGIN_RESPONSE:
                ResponseCodeEnum loginResponse = message.getCodeEnum();
                UserInfo userInfo = (UserInfo)message.getAttribute().get(ResponseEnum.LOGIN_RESPONSE.getAttr());
                MessageHandler.loginHandler(channel,loginResponse,userInfo);
                break;
            case WORLD_TALK_RESPONSE:
                String worldTalkResponse = (String)message.getAttribute().get(ResponseEnum.WORLD_TALK_RESPONSE.getAttr());
                MessageHandler.worldTalkHandler(channel,worldTalkResponse);
                break;
            case PRIVATE_TALK_RESPONSE:
                PrivateTalkModel privateTalkResponse = (PrivateTalkModel)message.getAttribute().get(ResponseEnum.PRIVATE_TALK_RESPONSE.getAttr());
                MessageHandler.privateTalkHandler(channel,privateTalkResponse.getUserInfo(),privateTalkResponse.getMessage());
                break;
            case ONLINE_RESPONSE:
                String onlineResponse = (String)message.getAttribute().get(ResponseEnum.ONLINE_RESPONSE.getAttr());
                MessageHandler.onlineHandler(channel,onlineResponse);
                break;
            case OFFLINE_RESPONSE:
                String offlineResponse = (String)message.getAttribute().get(ResponseEnum.OFFLINE_RESPONSE.getAttr());
                MessageHandler.onlineHandler(channel,offlineResponse);
                break;
            case ONLINE_LIST_RESPONSE:
                UserInfo[] loginUser = (UserInfo[])message.getAttribute().get(ResponseEnum.ONLINE_LIST_RESPONSE.getAttr());
                MessageHandler.onlineListHandler(channel,loginUser);
                break;
            case NOTICE_RESPONSE:
                String notice = (String)message.getAttribute().get(ResponseEnum.NOTICE_RESPONSE.getAttr());
                MessageHandler.noticeHandler(channel,notice);
                break;
            default:
                System.out.println("收到一条未知消息-"+message.getMessageType().getMessageType());
                break;
        }
    }

}
